﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using HotelManager.Data_Transfer_Layer;

namespace HotelManager.Data_Access_Layer
{
    class chitiethoadondichvuDAO
    {
        #region Variables
        private OleDbConnection conn;
        private string error;
        #endregion

        #region Properties
        public string Error
        {
            get { return error; }
            set { error = value; }
        }
        #endregion

        public chitiethoadondichvuDAO()
        {
            error = "";
            try
            {
                string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\hotel.accdb";
                conn = new OleDbConnection(strConn);
                conn.Open();
            }
            catch (Exception ex)
            {
                Error = "Không kết nối được đến cơ sở dữ liêu";
            }
        }

        public chitiethoadondichvuDTO[] get()
        {
            chitiethoadondichvuDTO[] objs;
            objs = new chitiethoadondichvuDTO[100];
            try
            {
                string sql = "SELECT * FROM chitiethoadondichvu";
                OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                int i = 0, n = dt.Rows.Count;
                objs = new chitiethoadondichvuDTO[n];
                while (reader.Read())
                {
                    objs[i] = new chitiethoadondichvuDTO();
                    objs[i].Hd_id = reader.GetInt32(0);
                    objs[i].Dv_id = reader.GetInt32(1);
                    objs[i].Cthddv_soluong = reader.GetInt32(2);
                    objs[i].Cthddv_gia = reader.GetInt32(3);
                    objs[i].Cthddv_tgsudung = reader.GetString(4).ToString();
                    i++;
                }
            }
            catch (Exception ex)
            {

            }
            return objs;
        }

        public chitiethoadondichvuDTO View_Detail(int hd_id)
        {
            chitiethoadondichvuDTO obj = new chitiethoadondichvuDTO();
            try
            {
                string sql = "SELECT * FROM chitiethoadondichvu WHERE hd_id=" + hd_id;
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                OleDbDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    obj.Hd_id = reader.GetInt32(0);
                    obj.Dv_id = reader.GetInt32(1);
                    obj.Cthddv_soluong = reader.GetInt32(2);
                    obj.Cthddv_gia = reader.GetInt32(3);
                    obj.Cthddv_tgsudung = reader.GetString(4).ToString();
                }
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
            return obj;
        }

        public void Delete(chitiethoadondichvuDTO obj)
        {
            try
            {
                string sql = "DELETE FROM chitiethoadondichvu WHERE hd_id=" + obj.Hd_id;
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }

        public void Insert(chitiethoadondichvuDTO obj)
        {
            try
            {
                string sql = "INSERT INTO chitiethoadondichvu VALUES(''," + obj.Dv_id + "," + obj.Cthddv_soluong + "," + obj.Cthddv_gia + ",'" + obj.Cthddv_tgsudung + "')";
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }

        public void Update(chitiethoadondichvuDTO obj)
        {
            try
            {
                string sql1 = "UPDATE chitiethoadondichvu SET dv_id=" + obj.Dv_id + ",cthddv_soluong=" + obj.Cthddv_soluong + ",cthddv_gia=" + obj.Cthddv_gia + ",cthddv_tgsudung='" + obj.Cthddv_tgsudung + "' WHERE hd_id=" + obj.Hd_id;
                OleDbCommand cmd = new OleDbCommand(sql1, conn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.ToString();
            }
        }
    }
}
